import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Test from '../components/Test'

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/login'},
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect: '/users',
    children:[
      {
        path:'/welcome',
        component:Welcome
      },
      {
        path:'/users',
        component:Users
      },
      {
        path:'/test',
        component:Test
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  // next 是放行
  if(to.path === '/login' || to.path === '/test'){
    return next();
  }
  const token = window.sessionStorage.getItem("token");
  if(!token){
    return next('/login');
  }
  next();
})
export default router
